// pages/shopManager/shopBuy/shopBuy.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopType: ['无', '城主', '区级代理', '市级代理', '省级代理'],
        quotaList: [],
        userInfo: null,
        shopData: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.cloud.init({
            env: 'bl-cloud-jc35g'
        })
        const that = this
        const db = wx.cloud.database()
        db.collection('shopQuota').get().then(res => {
            that.setData({
                quotaList: res.data
            })
        })
        this.getUserInfo()
        this.getShopData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
            }
        }).catch(function(e) {})
    },

    getShopData: function() {
        const that = this
        let data = {}
        common.post('Storequota/get_store_quota', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopData: res
                })
            }
        }).catch(function(e) {})
    },

    goMyShop: function() {
        const that = this
        if (this.data.userInfo.store_info != 3) {
            wx.navigateTo({
                url: '/pages/shopManager/shopAudit/step0/step0'
            })
        } else {
            wx.navigateToMiniProgram({
                appId: 'wxe7e8543c7f7121d8',
                path: '/pages/index/index?storeId=' + that.data.userInfo.real_store_id,
                success(res) {
                    // 打开成功
                    console.log(res)
                }
            })
        }
    },

    goBuyQuota: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopQuota/shopQuota'
        })
    },

    goApplyList: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopEmpower/shopEmpower'
        })
    },

    goShopLegal: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopLegal/shopLegal'
        })
    },

    goShopBuyDetail: function(e) {
        wx.navigateTo({
            url: '/pages/shopManager/shopBuyDetail/shopBuyDetail?id=' + e.currentTarget.dataset.id
        })
    }
})